package com.wbt.flink.wc;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.operators.UnsortedGrouping;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class BatchWorkCount {

    public static void main(String[] args) throws Exception{
        // 1 创建环境
        ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
        // 2 从文件读取数据
        DataSource<String> lineDataSoure = environment.readTextFile("st_flink/input/works.txt");

        // 3 将每行数据分词
        FlatMapOperator<String, Tuple2<String, Long>> wordAndOneTuple = lineDataSoure.flatMap((String line, Collector<Tuple2<String, Long>> out) -> {

            String[] words = line.split(" ");
            for (String word : words) {
                out.collect(Tuple2.of(word, 1L));
            }
        }).returns(Types.TUPLE(Types.STRING, Types.LONG));

        // 4 按照word 进行分组
        UnsortedGrouping<Tuple2<String,Long>> wordAndOneGroup = wordAndOneTuple.groupBy(0);
        // 5 分组内进行聚会统计
        AggregateOperator<Tuple2<String,Long>> sum = wordAndOneGroup.sum(1);

        // 6 打印结果
        sum.print();
    }
}
